package com.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Aspect
public class MyAspect {

    //1、统计save方法执行时间:Around
    @Around("execution(public * com.service.UserService.save(..))")
    public Object countProcessTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.nanoTime();
        Object ret  = joinPoint.proceed();
        long end = System.nanoTime();

        System.out.println("执行时间(纳秒):"+( end - start ));

        return ret;
    }

    @Around("execution(* com.service.UserService.delete(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String d1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS").format(LocalDateTime.now());
        System.out.println("当前时间:"+d1);
        Object ret = joinPoint.proceed();
        String d2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS").format(LocalDateTime.now());
        System.out.println("当前时间:"+d2);

        Object[] objects = joinPoint.getArgs();
        for (Object o : objects) {
            System.out.println("参数为->"+o);
        }

        return ret;
    }

}
